/**
 * Created by Administrator on 2018/10/5 0005.
 */
$(function () {
    $("#ComListCheckAll").click(function () {

        if ($(this).children(".text").text() == "全选") {
            $(this).children(".text").text("取消");
            $(".checkall input:enabled").prop("checked", true);
        } else {
            $(this).children(".text").text("全选");
            $(".checkall input:enabled").prop("checked", false);
        }
        var form = layui.form;
        form.render();
    })
    $("#ComListDelete").click(function () {
        __doPostBack('multiDelete','')
    })
});


function log(data) {
    console.log(data);
}
function logJson(data) {
    console.log(JSON.stringify(data));
}

function showToast(text){
    parent.layer.msg(text)
}

function hideToast(){
    // appcan.window.closeToast();
}

function showConfirm (message,functionConfirm,functionCancel) {
    parent.layer.open({
        content: message
        ,btn: ['确认','取消']
        ,yes: function(index, layero){
            //按钮【按钮一】的回调
            functionConfirm()
        }
        ,btn2: function(index, layero){
            functionCancel()
        }
        ,cancel: function(){
            //右上角关闭回调
            layer.msg("已取消")
            functionCancel()
        }
    });
}


var form = layui.form;
var layer = layui.layer;
form.on('submit(mainform)', function(data){
    var postUrl = data.form.action;
    apiPost(postUrl,data.field,function (res) {
        console.log(res)
        if (res.d) {
            // showToast(res.i)
            showConfirm(res.i, function () {
                log(111)
                parent.layer.closeAll();
                goto(res.d)
            }, function () {
                parent.layer.closeAll();
                goto(res.d)
            });
        }else {
            showToast(res.i)
        }
    })
    return false;
});
function goto(url) {

    log("goto")
    location.href = url;
}

function goback() {
    history.go(-1)
}

function isFalse(param) {
    if (param == ''){
        return false
    }
    if (param ==[]) {
        return false
    }
    if (param == undefined){
        return false
    }
    if (param == {}){
        return false
    }
    if (param == 0){
        return false
    }
    if (param == "0"){
        return false
    }
    return true
}
function isTrue(param) {
    if (param == ''){
        return false
    }
    if (param ==[]) {
        return false
    }
    if (param == undefined){
        return false
    }
    if (param == {}){
        return false
    }
    if (param == 0){
        return false
    }
    if (param == "0"){
        return false
    }
    return true
}

function showPicInList(URL) {
    parent.layer.open({
        type: 1
        ,title: false //不显示标题栏
        ,closeBtn: false
        ,area: '60vw;'
        ,shade: 0.8
        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
        ,btn: ['关闭']
        ,btnAlign: 'c'
        ,moveType: 0 //拖拽模式，0或者1
        ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
        '<img src="'+URL+'" alt="" style="width: 100%;height: auto;" />' +
        '</div>'
        ,success: function(layero){
            var btn = layero.find('.layui-layer-btn');
            btn.find('.layui-layer-btn0').attr({

            });
        }
    });
}

//这个是用来创建对象用的，暂时不知道为啥
var Class = { // 全局静态类, 用于声明一个新的类并提供构造函数支持
    create: function() {
        return function() { // 返回一个函数, 代表着这个新声明的类的构造函数
            // 一个命名为initialize的函数将被这个类实现作为类的构造函数
            this.initialize.apply(this, arguments);// initialize函数将在你实例化一个变量的时候被调用执行(即上面7个步骤中的第5步)
        }
    }
}
if (!Object.assign) {
    Object.defineProperty(Object, "assign", {
        enumerable: false,
        configurable: true,
        writable: true,
        value: function (target, firstSource) {
            "use strict";
            if (target === undefined || target === null)
                throw new TypeError("Cannot convert first argument to object");
            var to = Object(target);
            for (var i = 1; i < arguments.length; i++) {
                var nextSource = arguments[i];
                if (nextSource === undefined || nextSource === null) continue;
                var keysArray = Object.keys(Object(nextSource));
                for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) {
                    var nextKey = keysArray[nextIndex];
                    var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey);
                    if (desc !== undefined && desc.enumerable) to[nextKey] = nextSource[nextKey];
                }
            }
            return to;
        }
    });
}


function createForm(en) {
    var form = {};
    logJson(en)
    for(var i in en){
        form["form[" + i +"]"] = en[i]
    }
    if (en.id !== undefined && isTrue(en.id)) {
        form["id"] = en.id
    }
    return form;
}
function recreateEn(item) {
    var data= {}
    for (var i in item) {
        var keys = i.split("_")
        var str = ""
        keys.map(function (data, index) {
            if (index !== 0){
                str += data
            }
        })
        data[ str ] = item[i]
    }
    return data
}

function clearEn(en) {
    var data= {}
    for (var i in en) {
        data[ i ] = ""
    }
    return data
}

function getDate() {
    return new Date().getFullYear() + "-" + ((new Date().getMonth()+1)<10?"0"+(new Date().getMonth()+1):(new Date().getMonth()+1)) + "-" + new Date().getDate()
}
function apiGet( url , data , func){
    log("进入apiGet,正在获取数据")
    logJson(data)
    $.ajax({
        url :  url,
        type : 'GET',
        data : data,
        headers: {
            // cookie: cookie()
        },
        success : function(data, status,requestCode,response, xhr) {
            // var cookie = getCookie(response)
            // if(typeof cookie == "string"){
            //     setLocData("cookie",cookie);
            // }
            // try{
            //     var res = JSON.parse(data)
            //     logJson(res);
            // }catch(e){
            //     showToast("数据异常");
            //     return;
            // }finally{
            // }
            // apiDealWithResult(res,func)
            func(data,requestCode.status)
        },
        error : function(e) {

        }
    });

}


function apiPost( url , data ,func){
    log("进入apiGet,正在获取数据")
    log(url)
    logJson(data)
    var method = "POST"
    // if (isTrue(data.id)) {
    //     method = "PATCH"
    //     url += "/" + data.id
    // }
    $.ajax({
        url :  url,
        type : method,
        data : data,
        headers: {
            // contentType:'application/json',
            // cookie: cookie()
        },
        success : function(data, status,requestCode,response, xhr) {
            // console.log(status)
            // console.log(requestCode.status)
            // console.log(response)
            // console.log(xhr)
            //log("Api Success Log")
            //log("response")
            //log(response)
            //log("data")
            //log(data)
            // var cookie = getCookie(response)
            // if(typeof cookie == "string"){
            //     setLocData("cookie",cookie);
            // }
            func(data,requestCode.status)
        },
        error : function(e) {

        }
    });
}


function uploadOnePic (data ,func) {
    log("uploadOnePic")
    if (toOSS == 1 ) {
        uploadOnePicToOss(data,func)
    }else{
        var param = {Filedata:{path:data}}
        apiPost( "/MyWebPicUpload" , param ,func)
    }


}
